package com.eelpo.framework.socket;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.compression.ZlibEncoder;
import org.jboss.netty.handler.execution.ExecutionHandler;

import com.eelpo.framework.socket.handler.SocketFrameDecoder;
import com.eelpo.framework.socket.handler.SocketIdleStateHandler;
import com.eelpo.framework.socket.handler.SocketProcessHandler;

/**
 * Socket管线工厂
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 * @since 1.0 Create on May 30, 2012
 */
public class SocketPipelineFactory implements ChannelPipelineFactory {

	private ExecutionHandler executionHandler;
	private SocketProcessHandler socketProcessHandler;
	private SocketIdleStateHandler socketIdleStateHandler;

	public SocketPipelineFactory() {

	}

	public SocketPipelineFactory(ExecutionHandler executionHandler, SocketProcessHandler socketProcessHandler, SocketIdleStateHandler socketIdleStateHandler) {
		this.executionHandler = executionHandler;
		this.socketProcessHandler = socketProcessHandler;
		this.socketIdleStateHandler = socketIdleStateHandler;
	}

	@Override
	public ChannelPipeline getPipeline() throws Exception {
		ChannelPipeline pipeline = Channels.pipeline();
		pipeline.addLast("hearbeat", getSocketIdleStateHandler());
		pipeline.addLast("execution", getExecutionHandler());
		pipeline.addLast("frameDecoder", new SocketFrameDecoder());
		pipeline.addLast("zlibEncoder", new ZlibEncoder());
		pipeline.addLast("processHandler", getSocketProcessHandler());
		return pipeline;
	}

	public ExecutionHandler getExecutionHandler() {
		return executionHandler;
	}

	public void setExecutionHandler(ExecutionHandler executionHandler) {
		this.executionHandler = executionHandler;
	}

	public SocketProcessHandler getSocketProcessHandler() {
		return socketProcessHandler;
	}

	public void setSocketProcessHandler(SocketProcessHandler socketProcessHandler) {
		this.socketProcessHandler = socketProcessHandler;
	}

	public SocketIdleStateHandler getSocketIdleStateHandler() {
		return socketIdleStateHandler;
	}

	public void setSocketIdleStateHandler(SocketIdleStateHandler socketIdleStateHandler) {
		this.socketIdleStateHandler = socketIdleStateHandler;
	}

}
